<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="池子名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入池子名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentType">
        <el-select v-model="queryParams.equipmentType" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.equipment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态" prop="b2">
        <el-select v-model="queryParams.b2" placeholder="请选择设备状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:pool:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:pool:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:pool:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:pool:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="poolList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="池子名称" align="center" prop="equipmentName" />
      <el-table-column label="设备类型" align="center" prop="equipmentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equipment_type" :value="scope.row.equipmentType"/>
        </template>
      </el-table-column>
      <el-table-column label="池子深度(厘米)" align="center" prop="totDepth" />
      <el-table-column label="液位计上限(厘米)" align="center" prop="measurementUp" />
      <el-table-column label="液位计下限(厘米)" align="center" prop="measurementLow" />
      <el-table-column label="时间设置(天)" align="center" prop="timeOut" />
      <el-table-column label="进口阀门状态(实时)" align="center" prop="inletValve">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equipment_status" :value="scope.row.inletValve"/>
        </template>
      </el-table-column>
      <el-table-column label="出口阀门状态(实时)" align="center" prop="outletValve">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equipment_status" :value="scope.row.outletValve"/>
        </template>
      </el-table-column>
      <el-table-column label="当前液位高度(实时)厘米" align="center" prop="currentAltitude" />
      <el-table-column label="沉淀开始时间(实时)" align="center" prop="effectiveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="水泵状态" align="center" prop="pumpStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equipment_status" :value="scope.row.pumpStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="报警浮球状态" align="center" prop="ballStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equipment_status" :value="scope.row.ballStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="辐射值" align="center" prop="b1" />
      <el-table-column label="设备状态" align="center" prop="b2">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.b2"/>
        </template>
      </el-table-column>
      <el-table-column label="备用" align="center" prop="b3" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:pool:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:pool:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改池子设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="池子名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入池子名称" />
        </el-form-item>
        <el-form-item label="设备类型" prop="equipmentType">
          <el-select v-model="form.equipmentType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.equipment_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="池子深度(厘米)" prop="totDepth">
          <el-input v-model="form.totDepth" placeholder="请输入池子深度(厘米)" />
        </el-form-item>
        <el-form-item label="液位计上限(厘米)" prop="measurementUp">
          <el-input v-model="form.measurementUp" placeholder="请输入液位计上限(厘米)" />
        </el-form-item>
        <el-form-item label="液位计下限(厘米)" prop="measurementLow">
          <el-input v-model="form.measurementLow" placeholder="请输入液位计下限(厘米)" />
        </el-form-item>
        <el-form-item label="时间设置(天)" prop="timeOut">
          <el-input v-model="form.timeOut" placeholder="请输入时间设置(天)" />
        </el-form-item>
        <el-form-item label="进口阀门状态(实时)" prop="inletValve">
          <el-input v-model="form.inletValve" placeholder="请输入进口阀门状态(实时)" />
        </el-form-item>
        <el-form-item label="出口阀门状态(实时)" prop="outletValve">
          <el-input v-model="form.outletValve" placeholder="请输入出口阀门状态(实时)" />
        </el-form-item>
        <el-form-item label="当前液位高度(实时)厘米" prop="currentAltitude">
          <el-input v-model="form.currentAltitude" placeholder="请输入当前液位高度(实时)厘米" />
        </el-form-item>
        <el-form-item label="沉淀开始时间(实时)" prop="effectiveTime">
          <el-date-picker clearable
            v-model="form.effectiveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择沉淀开始时间(实时)">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="水泵状态" prop="pumpStatus">
          <el-radio-group v-model="form.pumpStatus">
            <el-radio
              v-for="dict in dict.type.equipment_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报警浮球状态" prop="ballStatus">
          <el-radio-group v-model="form.ballStatus">
            <el-radio
              v-for="dict in dict.type.equipment_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="辐射值" prop="b1">
          <el-input v-model="form.b1" placeholder="请输入辐射值" />
        </el-form-item>
        <el-form-item label="设备状态" prop="b2">
          <el-radio-group v-model="form.b2">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备用" prop="b3">
          <el-input v-model="form.b3" placeholder="请输入备用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPool, getPool, delPool, addPool, updatePool } from "@/api/business/pool";

export default {
  name: "Pool",
  dicts: ['equipment_status', 'equipment_type', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 池子设备管理表格数据
      poolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: null,
        equipmentType: null,
        b2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询池子设备管理列表 */
    getList() {
      this.loading = true;
      listPool(this.queryParams).then(response => {
        this.poolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentName: null,
        equipmentType: null,
        totDepth: null,
        measurementUp: null,
        measurementLow: null,
        timeOut: null,
        inletValve: null,
        outletValve: null,
        currentAltitude: null,
        effectiveTime: null,
        pumpStatus: null,
        ballStatus: null,
        b1: null,
        b2: null,
        b3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加池子设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPool(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改池子设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePool(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPool(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除池子设备管理编号为"' + ids + '"的数据项？').then(function() {
        return delPool(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/pool/export', {
        ...this.queryParams
      }, `pool_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
